import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;



public class PanelIzquierdo extends JPanel{

	
	private static final long serialVersionUID 	= 4L;
	private ServidorBaseDatos baseDatos			;
	private VentanaPrincipal ventanaPrincipal	;
	
	private JButton agenda						= new JButton("Agenda");
	
	private String nombresOpciones[]			= {"Usuarios","Empleados","Inmuebles","Clientes",
													"Ventas","Alquileres","Notas de seguimiento",
													"Recordatorios y citas","Garantias"};
	private JList listaOpciones					= new JList(nombresOpciones);
	private static JPanel panel[]						;
	
	
	public PanelIzquierdo(ServidorBaseDatos baseDatos, VentanaPrincipal ventanaPrincipal){
	
		this.baseDatos=baseDatos;
		this.ventanaPrincipal=ventanaPrincipal;
		this.setLayout(new GridBagLayout());
		agregarBorde();
		
		panel				= new JPanel[9];
		
		for(int i=0;i<9;i++){
			panel[i]=new JPanel();
		}
		panel[3]			= new PanelDerechoCliente(baseDatos);
		panel[1]			= new PanelDerechoEmpleado(baseDatos);
		
		agregarOpciones();
		agregarAgenda();
		
		
		ListSelectionListener eventoCambiarPanel = new ListSelectionListener(){
			public void valueChanged(ListSelectionEvent arg0) {
				cambiarPanelDerecho(); 
			} 
		};
		listaOpciones.addListSelectionListener(eventoCambiarPanel);
		
		/*
		ActionListener eventoCambiarPanel1 = new ActionListener(){ 
			public void actionPerformed(ActionEvent e){
				cambiarPanelDerechoAgenda();
			}
		};
		agenda.addActionListener(eventoCambiarPanel1);*/
	}
	
	protected void cambiarPanelDerecho() {
		
		try{
			ventanaPrincipal.cambiarPanelDerecho(panel[listaOpciones.getSelectedIndex()]);
			
		}catch (ArrayIndexOutOfBoundsException nulo){
			ventanaPrincipal.cambiarPanelDerecho(new JPanel());	
		}catch (NullPointerException nulo){
			//ventanaPrincipal.cambiarPanelDerecho(panel[3]);
		}
	}

	protected void cambiarPanelDerechoAgenda() {
		ventanaPrincipal.cambiarPanelDerecho(new PanelDerechoDefault(baseDatos));
	}

	private void agregarBorde(){
		
		Border raisedbevel, loweredbevel;
		raisedbevel = BorderFactory.createRaisedBevelBorder();
		loweredbevel = BorderFactory.createLoweredBevelBorder();
		Border compound;
		compound = BorderFactory.createCompoundBorder(raisedbevel, loweredbevel);
		this.setBorder(compound);
	}
	private void agregarOpciones(){
			
		listaOpciones.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listaOpciones.setVisibleRowCount(10);
		
		GridBagConstraints constraints = new GridBagConstraints();
				
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridwidth = 3;
		constraints.gridheight = 1;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weightx = 1.0;
		constraints.weighty = 0.0;
		
		JScrollPane listScroller = new JScrollPane(listaOpciones);
		listScroller.setPreferredSize(new Dimension(10,200));
		this.add (listScroller, constraints);
				
	}
	private void agregarAgenda(){
		
		GridBagConstraints constraints = new GridBagConstraints();
		
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridwidth = 3;
		constraints.gridheight = 2;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weighty = 1.0; 
		constraints.weightx = 1.0; 
		this.add (agenda, constraints); 
		
	}
	
	public static void reCargarCombosPaneles(){
		((reCargaDatos)panel[3]).reCarga();
		((reCargaDatos)panel[1]).reCarga();
	}
}
